package com.lonelyzhe.springboot.service.impl;

import com.lonelyzhe.springboot.bean.Book;
import com.lonelyzhe.springboot.bean.OrderInfo;
import com.lonelyzhe.springboot.bean.OrderItem;
import com.lonelyzhe.springboot.bean.User;
import com.lonelyzhe.springboot.bean.vo.Item;
import com.lonelyzhe.springboot.bean.vo.OrderInfoWithItem;
import com.lonelyzhe.springboot.mapper.CartMapper;
import com.lonelyzhe.springboot.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartService implements ICartService {
    @Autowired
    CartMapper cartMapper;

    @Override
    public void addItem(Item item) {
        // 查找一个是否有状态为0的订单
        OrderInfo cart = cartMapper.selectCart(item.getUser_id());
        if(cart == null){   // 没有找到则新建一个
//            System.out.println("没找到，新建了一个");
            // 新建一个订单
            OrderInfo initOrder = new OrderInfo();
            initOrder.setUser(new User());
            initOrder.getUser().setId(item.getUser_id());
            cartMapper.createCart(initOrder);
            int cart_id = initOrder.getId();
            // 新建一个图书项目
            OrderItem orderItem = new OrderItem();
            orderItem.setNum(item.getBook_num());
            orderItem.setOrder_id(cart_id);
            orderItem.setBook(new Book());
            orderItem.getBook().setId(item.getBook_id());
            cartMapper.insertItem(orderItem);
        }else {
            // 找到了购物车，则先检查有没有和该项目相同的项目
            OrderItem exitItem = cartMapper.selectItemOfOrder(cart.getId(),item.getBook_id());
            if(exitItem == null){
                // 新建一个图书项目
                OrderItem orderItem = new OrderItem();
                orderItem.setNum(item.getBook_num());
                orderItem.setOrder_id(cart.getId());
                orderItem.setBook(new Book());
                orderItem.getBook().setId(item.getBook_id());
                cartMapper.insertItem(orderItem);
            }else{
                // 订单中有同一个项目的更新数量即可
                cartMapper.updateItem(exitItem.getId(),item.getBook_num()+exitItem.getNum());
            }

        }
    }

    @Override
    public OrderInfoWithItem selectCart(int id) {
        OrderInfo order = cartMapper.selectCart(id);
        if (order != null){
            List<OrderItem> items = cartMapper.selectCartItems(order.getId());
            OrderInfoWithItem orderInfo = new OrderInfoWithItem();
            orderInfo.setOrder_info(order);
            orderInfo.setItems(items);
            return orderInfo;
        } else {
            OrderInfo initOrder = new OrderInfo();
            initOrder.setUser(new User());
            initOrder.getUser().setId(id);
            cartMapper.createCart(initOrder);
            order = cartMapper.selectCart(id);
            OrderInfoWithItem orderInfo = new OrderInfoWithItem();
            orderInfo.setOrder_info(order);
            orderInfo.setItems(null);
            return orderInfo;
        }
    }

    @Override
    public void changeItemNum(int item_id, int num) {
        try{
            cartMapper.changeItemNum(item_id,num);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public void submitOrder(int order_id) {
        try {
            cartMapper.submitOrder(order_id);
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void deleteItem(int item_id) {
        cartMapper.deleteItem(item_id);
    }
}
